<?php
	$title='Fitness article';
	$description='Fitness articles - view article';
	$keywords='fitness, article';
?>
<?php include('fns_db_articles.php'); ?>
<?php include('../views/layouts/_header.php'); ?>
<?php include('../views/layouts/_left.php'); ?>
<script language="Javascript">
function showAlert(){
	alert ("This is a Javascript Alert")
}

function showOrHideComments(){
	if( document.getElementById('comments') ){

		var whichEl = document.getElementById('comments');

		if( whichEl.style.display == 'block' ){

			whichEl.style.display = 'none';
		}
		else{
			whichEl.style.display = 'block';
		}
	}
}
</script>
<?php
session_start();

if (isset($_GET['id'])) {$id = $_GET['id'];}

$cxn = db_connect();


if (isset($_GET['rated'])){

	if(!isset($_COOKIE["Fitness[$id]"])){
		if(isset($_SESSION['rated'])){
			$set_array = $_SESSION['rated'];
			if(!isset($set_array[$id])){
				$rating = $_POST['rating'];
				if($rating != null){
					vote_for_article($rating, $id);

					$set_array[$id] = "true";
					$_SESSION['rated']=$set_array;
					setcookie("Fitness[$id]", "rated", time()+ 60*60*24*7);
				}
			}
		}
		else{
			$rating = $_POST['rating'];
			if($rating != null){
				vote_for_article($rating, $id);

				$my_array=array($id => "true");
				$_SESSION['rated']=$my_array;
				setcookie("Fitness[$id]", "rated", time()+ 60*60*24*7);
			}
		}
	}

	//print_r and var_dump. These functions displays tha array as key-value pairs
	/*foreach($_SESSION['rated'] as $key=>$value)
	{
	// and print out the values
	echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
	}*/
}

$myrow = get_article_by_id($id);

if($myrow['votes'] != 0){
	$current_rating = $myrow['total_rating']/$myrow['votes'];
}else{
	$current_rating = 0;
}
// Assign star image
if ($current_rating <= 0  ){$path_to_stars = "/images/rating/stars2/00star.gif";}
if ($current_rating >= 0.5){$path_to_stars = "/images/rating/stars2/05star.gif";}
if ($current_rating >= 1  ){$path_to_stars = "/images/rating/stars2/1star.gif";}
if ($current_rating >= 1.5){$path_to_stars = "/images/rating/stars2/15star.gif";}
if ($current_rating >= 2  ){$path_to_stars = "/images/rating/stars2/2star.gif";}
if ($current_rating >= 2.5){$path_to_stars = "/images/rating/stars2/25star.gif";}
if ($current_rating >= 3  ){$path_to_stars = "/images/rating/stars2/3star.gif";}
if ($current_rating >= 3.5){$path_to_stars = "/images/rating/stars2/35star.gif";}
if ($current_rating >= 4  ){$path_to_stars = "/images/rating/stars2/4star.gif";}
if ($current_rating >= 4.5){$path_to_stars = "/images/rating/stars2/45star.gif";}
if ($current_rating >= 5  ){$path_to_stars = "/images/rating/stars2/5star.gif";}
//else {$path_to_stars = "/fitness/images/rating/stars2/00star.gif";}
?>

<label class="heading1" id="article_title"><?php echo $myrow['title']; ?></label>
<p>
	<?php
	if(isset($myrow['img_path']) && $myrow['img_path'] != ""){
		printf('<img src="../images/articles/thumbnails/%s" style="float:center;" border="2" align="texttop" onclick="window.open(\'/images/articles/%s\');"/>"', $myrow['img_path'], $myrow['img_path']);
	}

	echo '<pre class="article_text">';
	//$line = str_replace("\n","\n<br>",$myrow['text']);
	echo stripslashes($myrow['text']);
	?>
	</pre>

</p>
<p>Author: <span class='article_author'><?php echo $myrow['author']; ?></span>, Date Added: <?php echo $myrow['date']; ?></p>

<form name="rating" action="<?php $_SERVER['PHP_SELF']; ?>?id=<?php echo $id; ?>&rated=true" method="post">

<img src="<?php echo $path_to_stars ?>" border="2" align="absmiddle"/>

<?php
if(!isset($_SESSION['rated'][$id])){
	echo '' .
	'<select name="rating" size="1">
		<option value="" selected="true"></option>
        <option value="0">sucks</option>
        <option value="1">very bad</option>
        <option value="2">bad</option>
        <option value="3">good</option>
		<option value="4">very good</option>
        <option value="5">awesome</option>
    </select>
    <input type="submit" name="submit_vote" value="Rate it!">';
}

echo '</form><br/>';

//comments section 
echo '<a href="javascript:showOrHideComments();">Show/Hide/Add Comments</a>';
echo '<span id="commentsCount"></span>';

echo '<form name="comments" id="comments" style="display:none" action="'.$_SERVER['PHP_SELF'].'?id='.$id.'&&addComment=true" method="post">';

echo '<p><table>';
echo '<tr><td>Comment</td><td><textarea type="text" name="comment"  cols="45" rows="6"></textarea></td></tr>';
echo '<tr><td>Author </td><td><input type="text" name="author" size="51"></td></tr>';
echo '</table>';
echo '<input type="submit" name="submit_comment" value="Post Comment!"></p>';
if (isset($_GET['addComment']) && $_GET['addComment'] == "true")
{
	if (isset($_POST['comment']))
	{
		$comment = $_POST['comment'];

		if ($comment == '')
		{
			unset($comment);
		}
	}
	if (isset($_POST['author']))
	{
		$author = $_POST['author'];

		if ($author == '')
		{
			unset($author);
		}
	}
	if (isset($comment) && isset($author))
	{
		$result = insert_comment_for_article($comment, $author, $id);

		if ($result == 'true')
		{
			echo "<p>Comment added successfully!</p>";
		}
		else
		{
			echo "<p>Comment not added!</p>";
		}
	}
}

$allComments = get_all_comments_for_article($id);


//show the number of comments - adds text to previously added tag
$commentsCount=0;
if(mysqli_num_rows($allComments) > 0){
	$commentsCount = mysqli_num_rows($allComments);
}
$commentsCountText = '&nbsp;&nbsp;(There are '.$commentsCount.' comments for this article)';
echo '<br/>
<script type="text/javascript">
function changeText(){
	document.getElementById(\'commentsCount\').innerHTML = \''.$commentsCountText.'\';
}
changeText();
</script>';


for( $i = 0; $i < mysqli_num_rows( $allComments ); $i++ )
{
	$myrow = mysqli_fetch_assoc( $allComments );
	printf ("<p>%s<br/>%s, %s", $myrow["text"], $myrow["author"],$myrow["date"]);
}
echo '</form>'

?>

<?php include('../views/layouts/_footer.php'); ?>
